package com.hmall.controller;

import com.hdmall.business.common.annotation.LoginRequired;
import com.hmall.config.JPushConst;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.model.MallPushMessage;
import com.hmall.model.SysUser;
import com.hmall.service.JpushService;
import com.hmall.util.TokenUtil;
import com.hmall.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author zhoutao
 */
@RestController
@RequestMapping("/jpush/")
@CrossOrigin(origins = "http://localhost:8088", maxAge = 3600)
@Api(value = "极光推送api", description = "极光推送api说明")
public class JpushController {

    private final JpushService jpushService;

    private final JedisPool jedisPool;

    TokenUtil tokenUtil = new TokenUtil();

    private final JPushConst jPushConst;

//    @Value("${spring.dev}")
//    String dev;

    @Autowired
    public JpushController(JpushService jpushService, JedisPool jedisPool, JPushConst jPushConst) {
        this.jpushService = jpushService;
        this.jedisPool = jedisPool;
        this.jPushConst = jPushConst;
    }

    /**
     * @Date 2018/11/2 11:09
     * @Param [token, type, pageNum, pageSize]
     * @return com.hmall.common.ServerResponse<com.hmall.vo.PageVo>
     * @Description // 列出消息列表
     */
    @LoginRequired
    @GetMapping("mobileUserMessage")
    @ApiOperation(value = "列出用户消息列表--手机端", notes = "systemMsg, orderMsg系统消息,订单消息")
    public ServerResponse<PageVo<MallPushMessage>> mobileUserMessage(String token, String type, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {

//        System.out.println(dev);
        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return jpushService.mobileUserMessage(user.getUsername().split("-")[0], type, pageNum, pageSize);
    }
    @LoginRequired
    @GetMapping("mobileHomeMessage")
    @ApiOperation(value = "消息列表主页--手机端")
    public ServerResponse mobileHomeMessage(String token) throws Exception {

        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return jpushService.mobileHomeMessage(user.getUsername().split("-")[0]);
    }


    @LoginRequired
    @GetMapping("mobileUserMessageCount")
    @ApiOperation(value = "列出用户消息数目--手机端")
    public ServerResponse mobileUserMessageCount(String token) throws Exception {


        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return jpushService.mobileUserMessageCount(user.getUsername().split("-")[0]);
    }
    @LoginRequired
    @PostMapping("mobileSetIsRead")
    @ApiOperation(value = "信息状态设置已读")
    public ServerResponse mobilSetIsRead(String token, Integer messageId) {

        jpushService.mobileSetIsRead(messageId);
        return ServerResponse.createBySuccess();
    }
    @LoginRequired
    @PostMapping("mobileSetAllRead")
    @ApiOperation(value = "设置信息全部已读")
    public ServerResponse mobileSetAllRead(String token, String type) throws Exception {
        
        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        
        return jpushService.mobileSetAllRead(user.getUsername().split("-")[0], type);
    }
    @LoginRequired
    @GetMapping(value = "mobileDeleteMsg")
    @ApiOperation(value = "删除消息")
    public ServerResponse mobileDeleteMsg(String token, Integer messageId) throws Exception {

        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return jpushService.mobileDeleteMsg(user.getUsername().split("-")[0], messageId);
    }


    @ApiOperation(value = "广播推送", notes = "包括极光推送和华为推送")
    @GetMapping(value = "AllPush")
    public void jPushAll(String title, String body, String type, String value) throws IOException {

        jpushService.jPushAll(title, body, type, value, jPushConst.JPUSH_MALL_SECRET, jPushConst.JPUSH_MALL_APPKEY, jPushConst.HUAWEI_MALL_SECRET, jPushConst.HUAWEI_MALL_APPID);
    }


    @PostMapping(value = "HuaWeiPush")
    @ApiOperation(value = "华为推送测试")
    public void huaWeiPush(String title, String body, String userName, String type, String value) throws IOException {

//        userName = preUsername(userName);
        jpushService.sendPushMessage(title, body, userName, type, value, jPushConst.HUAWEI_MALL_SECRET, jPushConst.HUAWEI_MALL_APPID, null);
    }


    //极光推送+华为推送
    //新增声音支持
    @ApiOperation(value = "针对某个用户进行推送")
    @PostMapping(value = "JPush")
    public ServerResponse jPushMethod(String title, String body, String userName, String type, String value) throws IOException {

//        userName = preUsername(userName);

        return jpushService.jPushMethod(title, body, userName, type, value, jPushConst.JPUSH_MALL_SECRET, jPushConst.JPUSH_MALL_APPKEY, jPushConst.HUAWEI_MALL_SECRET, jPushConst.HUAWEI_MALL_APPID);
    }

   /**
    * @Date 2018/11/9 11:27
    * @Param [customerUserId, date, status, value]
    * @return com.hmall.common.ServerResponse
    * @Description //
    *  针对办事处进行消息推送
    *     1.用户拿样申请
    *     2.普通订单申请
    *     3.付款超时提醒 12h
    *     4.付款消息提醒
    *     5.收货确认
    *     6.差评提示
    *     7.生产计划提交申请
    *     2018/10/17
    *     8.跟单员给财务发送消息提醒
    *     2018/10/31
    *     9.业务员给资金和跟单员发送消息提醒
    *     2018/11/29
    *     10.nc结束给业务员作极光推送
    *     推送绑定的业务员以及管理员
    *     way 1 pc
    *     way 2 手机
    *
    *     2019/1/21
    *     11.竞拍申请发出推送
    *     12.资质审核发出推送
    */
    @ApiOperation("办事处的消息推送")
    @PostMapping("backPush")
    public ServerResponse backPushMessage(Integer customerUserId, Integer status, String value) throws IOException, ParseException {

        //type 2
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        return jpushService.backPushMessage(customerUserId, date, status, value);
    }

    @ApiOperation("纤天下消息推送")
    @PostMapping("xtxPush")
    public ServerResponse xtxPushMessage(String userName, Integer status, String value) throws IOException, ParseException {

        //type 2
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        return jpushService.xtxPushMessage(userName, date, status, value);
    }

    @ApiOperation(value = "纤天下广播推送", notes = "纤天下包括极光推送和推送")
    @GetMapping(value = "xtxAllPush")
    public void xtxJPushAll(String title, String body, String value) throws IOException {

        jpushService.xtxJPushAll(title, body, value, jPushConst.JPUSH_MALL_SECRET, jPushConst.JPUSH_MALL_APPKEY);
    }


    @ApiOperation("华鼎支持消息推送")
    @PostMapping("sopPush")
    public ServerResponse feedbackJPushMessage(String userName, Integer status, String value) throws IOException, ParseException {
        //type 2
        return jpushService.feedbackPushMessage(userName, status, value);
    }

    @ApiOperation(value = "华鼎支持广播推送", notes = "华鼎支持广送和推送")
    @GetMapping(value = "sopAllPush")
    public void feedbackJPushAll(String title, String body, String value) throws IOException {

        jpushService.xtxJPushAll(title, body, value, "40bf58b358efe4c555690250", "71942b086fac751114938c15");
    }


}
